package com.tanhua.db.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.api.PeachSoundsApi;
import com.tanhua.db.mapper.SoundsMapper;
import com.tanhua.model.pojo.Sounds;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class PeachSoundsApiImpl implements PeachSoundsApi {

    @Autowired
    private SoundsMapper soundsMapper;

    /*保存声音数据*/
    @Override
    public void saveSounds(Sounds sounds) {
        soundsMapper.insert(sounds);
    }

    @Override
    public synchronized Sounds getOneSounds(Long userId) {
        LambdaQueryWrapper<Sounds> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Sounds::getIsUsed,0).ne(Sounds ::getUserId,userId).orderByDesc(Sounds::getCreated).last("limit 1");
        Sounds sounds = soundsMapper.selectOne(wrapper);
        if(sounds == null){
            return null;
        }
        /*将数据状态跟新为1*/
        sounds.setIsUsed(1);
        soundsMapper.updateById(sounds);
        return sounds;
    }

}
